﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

using DragDraw.Util;

namespace DragDraw.Tool
{
    public partial class PhotoLargeViewer : UserControl
    {
        internal int Index { get; set; }
        private List<BitmapImage> m_images = new List<BitmapImage>();

        public PhotoLargeViewer()
        {
            InitializeComponent();
            Index = 0;
        }

        public void Init()
        {
            this.Height = App.Current.Host.Content.ActualHeight - 100;
            Refresh();
        }

        internal void SetPhotoList(List<Photo> photos)
        {
            m_images.Clear();
            foreach (Photo p in photos)
            {
                try
                {
                    string url = p.ImgUrl;
                    if (url.Contains(@"http://") == false)
                        url = WidgetHelper.MapFileUrl(
                            OperationManager.Instance.CurrentSpace.OwnerUserID, url);
                    m_images.Add(new BitmapImage(new Uri(url)));
                }
                catch
                {
                    m_images.Add(new BitmapImage());
                }
            }
            Index = 0;
        }

        private void Refresh()
        {
            // image 0
            if (Index < 2)
                Rect0.Visibility = Visibility.Collapsed;
            else
            {
                Rect0.Visibility = Visibility.Visible;
                ImgList0.ImageSource = m_images[Index - 2];
            }
            // image 1
            if (Index < 1)
                Rect1.Visibility = Visibility.Collapsed;
            else
            {
                Rect1.Visibility = Visibility.Visible;
                ImgList1.ImageSource = m_images[Index - 1];
            }
            // image 2 && image main
            if (m_images.Count > 0)
            {
                ImgList2.ImageSource = m_images[Index];
                ImgMain.Source = m_images[Index];
            }
            // image 3
            if (Index >= m_images.Count - 1)
                Rect3.Visibility = Visibility.Collapsed;
            else
            {
                Rect3.Visibility = Visibility.Visible;
                ImgList3.ImageSource = m_images[Index + 1];
            }
            // image 4
            if (Index >= m_images.Count - 2)
                Rect4.Visibility = Visibility.Collapsed;
            else
            {
                Rect4.Visibility = Visibility.Visible;
                ImgList4.ImageSource = m_images[Index + 2];
            }
        }

        private void BtnPre_Click(object sender, RoutedEventArgs e)
        {
            if(m_images.Count == 0) return;
            Index = (Index + m_images.Count - 1) % m_images.Count;
            Refresh();
        }

        private void BtnNext_Click(object sender, RoutedEventArgs e)
        {
            if (m_images.Count == 0) return;
            Index = (Index + m_images.Count + 1) % m_images.Count;
            Refresh();
        }
    }
}
